import { postNoticeHttp } from '../../../../request/noticeHttp'
export const releasenotice = (map) => {
  let dialogTableVisible = ref(false)
  const textarea = ref('')
  let draw = ref()
  var source = ref()
  var data = ref([])
  var vectorData = 'gdbp://MapGisLocal/wuhan/sfcls/buffer'
  const vecLayer = new Zondy.Map.GdbpLayer('', [vectorData], {
    ip: 'localhost',
    port: 6163,
  })
  const handleRelease = () => {
    map.value.removeInteraction(draw.value)

    /* 创建画布 */
    source.value = new ol.source.Vector({})
    var layer = new ol.layer.Vector({
      source: source.value,
    })
    map.value.addLayer(layer)

    draw.value = createDraw({
      source: source.value,
      type: 'LineString',
    })
    map.value.addInteraction(draw.value)
    draw.value.on('drawend', function (evt) {
      const position = evt.feature.getGeometry().getCoordinates()
      var attr = []
      var service = {
        name: 'buffer',
        layerId: 0,
      }
      addLine({
        position,
        attr,
        service,
        vecLayer,
      })
      /* 清除画笔 */
      map.value.removeInteraction(draw.value)

      singleAnalysis()
    })
  }

  let resultName = null //分析结果

  const singleAnalysis = () => {
    /* 1、调用接口创建分析对象 */
    var clsBufBySR = new Zondy.Service.ClassBufferBySingleRing({
      ip: 'localhost',
      port: '6163',
      //经纬度坐标系下,缓冲区的半径单位为度
      leftRad: 0.001,
      rightRad: 0.001,
      //不允许根据属性字段设置缓冲区半径
      isByAtt: false,
    })
    /* 设置时间戳,希望分析结果是随机的图层 */
    resultName = vectorData + Date.now()
    //2、设置参数
    //设置源图层
    clsBufBySR.srcInfo = vectorData
    //设置分析结果图层
    clsBufBySR.desInfo = resultName
    //3、 执行分析
    clsBufBySR.execute(onSuccess, 'post', false, 'json')
  }
  function onSuccess(result) {
    if (result.succeed) {
      const resultLayer = new Zondy.Map.GdbpLayer('', [resultName], {
        ip: 'localhost',
        port: 6163,
      })
      // map.value.addLayer(resultLayer)
      const layer1 = result.results[0].Value
      overAnalysis(layer1)
    }
  }

  // /* 叠加分析 */
  let resultname1
  function overAnalysis(layer1) {
    var vectorData1 = 'gdbp://MapGisLocal/wuhan/sfcls/居民区'
    //2.1设置要叠加的多边形
    resultname1 = vectorData1 + Date.now()
    //2.2 创建叠加分析服务
    var overlayParam = new Zondy.Service.ClipByLayer({
      ip: 'localhost',
      port: '6163', //访问IGServer的端口号，.net版为6163，Java版为8089,
      //设置被叠加图层URL
      srcInfo1: vectorData1,
      //设置被叠加图层URL
      srcInfo2: layer1,
      //设置结果URL
      desInfo: resultname1,
    })
    //2.3 执行叠加分析
    //调用基类的execute方法,执行叠加分析,onSuccess为结果回调函数
    overlayParam.execute(onSuccess1, 'post', false, 'json', onError)
  }
  //2-4、分析结果显示到地图容器上
  function onSuccess1(result) {
    if (result.succeed) {
      map.value.removeLayer(vecLayer)
      const resultLayer = new Zondy.Map.GdbpLayer('', [resultname1], {
        ip: 'localhost',
        port: '6163',
      })
      map.value.addLayer(resultLayer)

      queryByAttribute(result.results[0].Value)
    }
  }
  function onError(err) {
  }

  function queryByAttribute(res) {
    //初始化查询结构对象，设置查询结构包含几何信息
    var queryStruct = new Zondy.Service.QueryFeatureStruct()
    queryStruct.IncludeGeometry = true
    //实例化查询参数对象
    var queryParam = new Zondy.Service.QueryByLayerParameter(res, {
      resultFormat: 'json',
      struct: queryStruct,
    })
    //设置查询分页号
    queryParam.pageIndex = 0
    //设置查询要素数目
    queryParam.recordNumber = 200

    //实例化地图文档查询服务对象
    var queryService = new Zondy.Service.QueryLayerFeature(queryParam, {
      ip: 'localhost',
      port: '6163', //访问IGServer的端口号，.net版为6163，Java版为8089
    })
    //执行查询操作，querySuccess为查询回调函数
    queryService.query(querySuccess, queryError)
  }

  //查询失败回调
  function queryError(e) {
    //停止进度条
    stopPressBar()
  }

  //查询成功回调
  function querySuccess(result) {
    delFeature()
    data.value = result.SFEleArray
    //停止进度条
    setTimeout(() => {
      dialogTableVisible.value = true
    }, 1500)
    data.value = result.SFEleArray
    delFeature()
  }

  function delFeature() {
    var deleteService = new Zondy.Service.EditDocFeature('buffer', '0', {
      ip: 'localhost',
      port: 6163,
    })

    let arr = []
    for (let i = 0; i < 100; i++) {
      arr.push(i)
    }
    deleteService.deletes(arr, onPntSuccess2)
  }

  function onPntSuccess2(result) {
    if (result) {
      source.value.clear()
    }
  }

  // 发布公告
  async function handleSub(id) {
    if (textarea.value == '') {
      ElMessage({
        message: '请填写公告内容',
        type: 'warning',
      })
    } else {
      const res = await postNoticeHttp(id, textarea.value)
      dialogTableVisible.value = false
      ElMessage({
        message: '发布公告成功',
        type: 'success',
      })
    }
  }

  return {
    handleRelease,

    dialogTableVisible,
    data,
    handleSub,
    textarea,
  }
}
